package com.dianning.web.action;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Company;
import com.dianning.bean.Money;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.Util;
import com.dianning.exception.DianningException;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.RemoteAddrAware;

public class GetPassword extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(GetPassword.class);

	private UserInfoService userInfoService;

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	private Map session;

	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}	
	
	private String username;
	private String password;
	private String question;
	private String answer;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}


	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String index(){
		this.setAction("getpass");
        return INPUT;
	}
	
	public String getpass(){
		//查找管理员
		UserInfo ui = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(ui == null) {
			this.addActionError(this.getText("error.password.user"));
			this.getSession().put("code", "user_error");
			return INPUT;
		}
		this.question = ui.getQuestion();
		this.setAction("setp2");
		return "step";
	}
	
	public String setp2(){
		UserInfo ui = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(ui == null) {
			this.setQuestion(ui.getQuestion());
			this.addActionError(this.getText("error.password.user"));
			this.getSession().put("code", "user_error");
			return "step";
		}
		if(this.answer.equalsIgnoreCase(ui.getAnswer())){
			this.setQuestion(ui.getQuestion());
			this.setPassword(ui.getPassword());
			this.getSession().put("code", "show_code");
		}else{
			this.setQuestion(ui.getQuestion());
			this.addActionError(this.getText("error.password.question"));
			this.getSession().put("code", "question_error");
		}
		return "step";
	}

}
